package com.hzjy.api.task;

import com.hzjy.api.beans.HzjyProduct;
import com.hzjy.api.beans.HzjyProject;
import com.hzjy.api.mapper.ApiProjectMapper;
import com.hzjy.api.service.impl.ApiProductServiceImpl;
import com.hzjy.common.utils.DateUtils;
import com.hzjy.common.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 挂牌超期自动下架
 */
@Component
public class TradingDownTask {
    private static final Logger log = LoggerFactory.getLogger(TradingDownTask.class);

    @Autowired
    private ApiProductServiceImpl apiProductServiceImpl;
    @Autowired
    private ApiProjectMapper apiProjectMapper;

    /**
     * 每天0点挂牌超期自动下架
     */
    @Transactional
    public void createHotTradingList() {
        log.info("===========================【开始挂牌超期自动下架任务】====================================");
        Date now = new Date();
        List<HzjyProduct> list = apiProductServiceImpl.queryAllByTradingSuccAndUpState();
        if (!CollectionUtils.isEmpty(list)) {
            for (HzjyProduct hzjyProduct : list) {
                Date time = DateUtils.addDays(hzjyProduct.getUpdateTime(), hzjyProduct.getTerm());
                if (now.after(time)) {
                    log.info("当前挂牌上架已过期，将自动下架. productId : " + hzjyProduct.getProductId() + ", productName : "
                            + hzjyProduct.getProductName());
                    hzjyProduct.setUpState(0);
                    PageData pd = new PageData();
                    pd.put("product_id", hzjyProduct.getProductId());
                    apiProductServiceImpl.lowTrading(pd);
                }
                if (hzjyProduct.getTradingType() == 1){
                    //项目挂牌
                    HzjyProject queryProject = new HzjyProject();
                    queryProject.setProductId(hzjyProduct.getProductId().toString());
                    List<HzjyProject> hzjyProjectList = apiProjectMapper.selectHzjyProjectList(queryProject);
                    HzjyProject hzjyProject = hzjyProjectList.get(0);
                    if (hzjyProject.getPermissionEndTime() != null){
                        if (now.after(hzjyProject.getPermissionEndTime())){
                            log.info("当前挂牌许可已过期，将自动下架. productId : " + hzjyProduct.getProductId() + ", productName : "
                                    + hzjyProduct.getProductName());
                            hzjyProduct.setUpState(0);
                            PageData pd = new PageData();
                            pd.put("product_id", hzjyProduct.getProductId());
                            apiProductServiceImpl.lowTrading(pd);
                        }
                    }
                }

            }
        }
        log.info("===========================【挂牌超期自动下架任务结束】====================================");
    }
}
